//
//  WQCForgetViewController.m
//  renxinghuan
//
//  Created by 王晓民 on 2018/3/27.
//  Copyright © 2018年 吴清翠. All rights reserved.
//

#import "WQCForgetViewController.h"
#import "configuration.h"
#import "UIButton+CountDown.h"
@interface WQCForgetViewController ()<UITextFieldDelegate>
/** 账号输入框*/
@property(nonatomic,strong)UITextField * UserTextField;
@property(nonatomic,strong)UITextField * codeTextField;

/** 密码*/
@property(nonatomic,strong)UITextField * passtexfield;


/** 再次输入密码*/
@property(nonatomic,strong)UITextField * Zpasstexfield;

/** 验证码*/
@property(nonatomic,strong)UITextField * codeTexField;

@end

@implementation WQCForgetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self setupNav];
    [self creatUI];
}
- (void)setupNav
{
    UIView * registerView = [[UIView alloc] init];
    registerView.frame = CGRectMake(0, 0, ScreenWidth, 64);
    registerView.backgroundColor = WYCGlobalBg;
    [self.view addSubview:registerView];
    
    
    //不是Nav。所以自己写的View和label
    UILabel * NavRigsterLabel = [[UILabel alloc] init];
    NavRigsterLabel.frame = CGRectMake(ScreenWidth / 2 - 150 * px, 40 * px, 300 * px, 64);
    NavRigsterLabel.textColor = [UIColor blackColor];
    NavRigsterLabel.font = [UIFont boldSystemFontOfSize:BigFont];
    NavRigsterLabel.textAlignment = NSTextAlignmentCenter;
    NavRigsterLabel.text = @"忘记密码";
    [registerView addSubview:NavRigsterLabel];

    //返回按钮
    UIButton *  backbutton = [[UIButton alloc] init];
    [backbutton setImage:[UIImage imageNamed:@"Nav_close"] forState:UIControlStateNormal];
    [backbutton addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    backbutton.frame = CGRectMake(40 * px, NavRigsterLabel.height / 3, 40 , 40);
    [registerView addSubview:backbutton];
}
-(void)creatUI
{
    
    UIView * phoneView = [[UIView alloc] init];
    phoneView.frame = CGRectMake(40 , NavHeight+ 245 * px, ScreenWidth-80, 44);
    phoneView.backgroundColor = WYCRGBColor(249,249, 249);
    phoneView.layer.cornerRadius = 25;
    [self.view addSubview:phoneView];
    //手机号
    UIImageView * iphonelabel = [[UIImageView alloc] init];
    iphonelabel.frame = CGRectMake(21, 11, 21 , 21);
    iphonelabel.image = [UIImage imageNamed:@"icon_phone"];
    [phoneView addSubview:iphonelabel];
    
    UITextField * userTextField = [[UITextField alloc] init];
    userTextField.frame = CGRectMake(CGRectGetMaxX(iphonelabel.frame) + 30 * px,10, phoneView.width-60, 20);
    userTextField.backgroundColor = [UIColor clearColor];
    userTextField.placeholder = @"请输入手机号";
    userTextField.tintColor = wycMain;
    userTextField.keyboardType = UIKeyboardTypeNumberPad;
    userTextField.clearButtonMode=UITextFieldViewModeAlways;
    [phoneView addSubview:userTextField];
    
    self.UserTextField = userTextField;
    
    
    //验证码
    UIView * codeView = [[UIView alloc] init];
    codeView.frame = CGRectMake(40 , phoneView.y+69, ScreenWidth-80,44);
    codeView.backgroundColor = WYCRGBColor(249,249, 249);
    codeView.layer.cornerRadius = 25;
    [self.view addSubview:codeView];
    
    UIImageView * codelabel = [[UIImageView alloc] init];
    codelabel.frame = CGRectMake(21, 11, 21, 21);
    codelabel.image = [UIImage imageNamed:@"code"];
    [codeView addSubview:codelabel];
    
    
    UITextField * codeTextField = [[UITextField alloc] init];
    codeTextField.frame = CGRectMake(CGRectGetMaxX(iphonelabel.frame) + 30 * px,10, (codeView.width - iphonelabel.width + 10 * px) / 2, 20);
    codeTextField.backgroundColor = [UIColor clearColor];
    codeTextField.placeholder = @"验证码";
    codeTextField.tintColor = wycMain;
    [codeView addSubview:codeTextField];
        self.codeTextField = codeTextField;
    
    UIButton * forgetButton = [[UIButton alloc] init];
    forgetButton.frame = CGRectMake(codeView.width-125, 8, 120, codeView.height-16);
    forgetButton.titleLabel.font = [UIFont systemFontOfSize:WyzFont];
    forgetButton.backgroundColor = wycMain;
    forgetButton.layer.cornerRadius = 15;
    [forgetButton setTitle:@"获取验证码" forState:UIControlStateNormal];
    [forgetButton addTarget:self action:@selector(codeButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [forgetButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [codeView addSubview:forgetButton];
    //密码
    UIView * userView = [[UIView alloc] init];
    userView.frame = CGRectMake(40 , codeView.y+69, ScreenWidth-80,44);
    userView.backgroundColor = WYCRGBColor(249,249, 249);
    userView.layer.cornerRadius = 25;
    [self.view addSubview:userView];
    
    //密码
    UIImageView * passlabel = [[UIImageView alloc] init];
    passlabel.frame = CGRectMake(21, 11, 21, 21);
    passlabel.image = [UIImage imageNamed:@"icon_password"];
    [userView addSubview:passlabel];
    
    
    UITextField * passTextField = [[UITextField alloc] init];
    passTextField.frame = CGRectMake(CGRectGetMaxX(codelabel.frame) + 30 * px,10, phoneView.width-60 , 20);
    passTextField.backgroundColor = [UIColor clearColor];
    passTextField.placeholder = @"请输入密码";
    passTextField.tintColor = wycMain;
    passTextField.secureTextEntry = YES;
    passTextField.clearButtonMode=UITextFieldViewModeAlways;
    [userView addSubview:passTextField];
       self.passtexfield = passTextField;
    

    
    
    
    
    //提交按钮
    UIButton * loginButton = [[UIButton alloc] init];
    loginButton.frame = CGRectMake(userView.x, CGRectGetMaxY(userView.frame) + 70, userView.width, 150 * px);
    loginButton.backgroundColor = wycMain;
    loginButton.layer.cornerRadius = 25;
    [loginButton setTitle:@"提交" forState:UIControlStateNormal];
    loginButton.titleLabel.font = [UIFont systemFontOfSize:62 * px];
    [loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [loginButton addTarget:self action:@selector(forgetButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:loginButton];
    

}

-(void)codeButtonClick:(UIButton *)button
{
    
    if (self.UserTextField.text.length == 0) {
        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"手机号不能为空"];
        [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];
        [SVProgressHUD setForegroundColor:[UIColor whiteColor]]; //字体颜色
        [SVProgressHUD setBackgroundColor:[UIColor blackColor]];   //背景颜色
        return;
    }
    
  
    else{

    [button startWithTime:59 title:@"点击重新获取" countDownTitle:@"s后重新获取" mainColor:wycMain countColor: [UIColor grayColor]];
        

        NSMutableDictionary * params = [NSMutableDictionary dictionary];
        params[@"mobile"] = self.UserTextField.text;
        params[@"type"] = @"fpwd";
        NSLog(@"%@",params);
        [HttpTool POST:Rcode params:params success:^(NSURLSessionDataTask *task, id responseObject) {

            if ([[responseObject objectForKey:@"code"] intValue] == 0) {
                NSDictionary * dict = responseObject[@"result"];
                
                WYCAccount * Account = [[WYCAccount alloc] initWithDic:dict];
                
                [WYCAccountTool saveAccount:Account];
                
                [WYCIsAppLoginTool saveIsAppLogin:YES];
                [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"获取验证码成功"];
                [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];
                [SVProgressHUD setForegroundColor:[UIColor whiteColor]]; //字体颜色
                [SVProgressHUD setBackgroundColor:[UIColor blackColor]];   //背景颜色

            }else{

                [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"获取验证码失败"];
                [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];
                [SVProgressHUD setForegroundColor:[UIColor whiteColor]]; //字体颜色
                [SVProgressHUD setBackgroundColor:[UIColor blackColor]];   //背景颜色

            }
            
        } fail:^(NSURLSessionDataTask *task, NSError *error) {
            [SVProgressHUD showWithStatus:Failed];
            [SVProgressHUD showImage:[UIImage imageNamed:@""] status:Failed];

        }];
        
    }
}

-(void)forgetButtonClick:(UIButton *)button
{
    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];
    [SVProgressHUD setForegroundColor:[UIColor whiteColor]]; //字体颜色
    [SVProgressHUD setBackgroundColor:[UIColor blackColor]];   //背景颜色
        if(self.UserTextField.text.length == 0)
        {
            [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"手机号不能为空"];
            return;
        }
       else if(self.codeTextField.text.length == 0)
        {
            [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"验证码不能为空"];
            return;
        }
        else if(self.passtexfield.text.length == 0)
        {
            [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"密码不能为空"];
            return;
        }
        else{
            NSString * passStr = [NSString stringWithFormat:@"%@",self.passtexfield.text];
            NSString *pas = [RSA encryptString:passStr publicKey:RSA_P_KEY];
            
    NSMutableDictionary * params = [NSMutableDictionary dictionary];
    params[@"mobile"] = self.UserTextField.text;
    params[@"code"] = self.codeTextField.text;
    params[@"password"] = pas;
            //RSA密码加密
           
    NSLog(@"%@",params);
    [HttpTool POST:forgetPassword params:params success:^(NSURLSessionDataTask *task, id responseObject) {
        
        if ([[responseObject objectForKey:@"code"] intValue] == 0) {
            

            [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"找回成功"];
            [self dismissViewControllerAnimated:YES completion:nil];
        
        }else{
            
            [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"找回失败"];
            
        }
        
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        [SVProgressHUD showWithStatus:Failed];
        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:Failed];
        
    }];
        }
}
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (textField == self.UserTextField) {
        if (textField.text.length > 10) {
            return NO;
        }
    }
    return YES;
}
-(void)backClick
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
@end
